package com.ts.opengl

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES30
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.nio.IntBuffer
import javax.microedition.khronos.opengles.GL

abstract class AbstractFilter {
    var VERTEX: FloatArray = floatArrayOf(
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f
    )



    var program=1;

    var mWidth: Int = 0

    var mHeight: Int = 0

    var vertexBuffer: FloatBuffer? = null //顶点坐标缓存区

    var  textureBuffer : FloatBuffer?= null;

    var vPosition: Int = 0

    var vCoord: Int = 0

    var vTexture: Int = 0

    protected var vMatrix: Int = 0

    constructor(context: Context,vertexShaderId:Int,fragmentShaderId:Int){

        val vertexShader=OpenGLUtils.readRawTextFile(context,vertexShaderId)

        val fragSharder=OpenGLUtils.readRawTextFile(context,fragmentShaderId)

        program=OpenGLUtils.loadProgram(vertexShader,fragSharder);
//        XCrash.init(this)

        vPosition = GLES20.glGetAttribLocation(program, "vPosition") //0

        //接收纹理坐标，接收采样器采样图片的坐标
        vCoord = GLES20.glGetAttribLocation(program, "vCoord") //1

        //采样点的坐标
        vTexture = GLES20.glGetUniformLocation(program, "vTexture")
        vMatrix = GLES20.glGetUniformLocation(program, "vMatrix")


        //        建立通道
        vertexBuffer =
            ByteBuffer.allocateDirect(4 * 4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer()
        vertexBuffer?.clear()
        vertexBuffer?.put(VERTEX)

//        textureBuffer = ByteBuffer.allocateDirect(4 * 4 * 2).order(ByteOrder.nativeOrder())
//            .asFloatBuffer()
//        textureBuffer?.clear()
//        textureBuffer?.put(TEXTURE)
    }

    fun setSize(width: Int, height: Int) {
        mWidth = width
        mHeight = height
    }
    public fun onDraw(texture:Int):Int {


        //设置原点 和视角
        GLES30.glViewport(0,0,mWidth,mHeight)
        ///使用程序


        //创建vbo
        val mVob=IntBuffer.allocate(1)
        GLES30.glGenBuffers(1,mVob);
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER,mVob.get(0));
//        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER,)
        //绑定vbo数据
        GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER,VERTEX.size,
            vertexBuffer,GLES30.GL_STATIC_DRAW)

        val mVao=IntBuffer.allocate(1)
        GLES30.glGenVertexArrays(1,mVao)
        GLES30.glBindVertexArray(mVao[0])

        //vbo和vao绑定
        GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER,mVob[0])

        glEnableVertexAttribArray(0);

        //顶点着色器
        GLES30.glUseProgram(program)
        var m_VaoId= IntBuffer.allocate(1)
        GLES30.glGenVertexArrays(1,m_VaoId)
//        GLES30.glBindVertexArray(m_VaoId[])
        vertexBuffer?.position(0)
        GLES30.glVertexAttribPointer(vPosition, 2, GLES30.GL_FLOAT, false, 0, vertexBuffer)
        GLES30.glEnableVertexAttribArray(vCoord)

         //片着色器
        textureBuffer?.position(0)
        GLES30.glVertexAttribPointer(
            vCoord, 2, GLES20.GL_FLOAT,
            false, 0, textureBuffer
        )
        //CPU传数据到GPU，默认情况下着色器无法读取到这个数据。 需要我们启用一下才可以读取
        GLES30.glEnableVertexAttribArray(vCoord)

        GLES30.glActiveTexture(GLES20.GL_TEXTURE0)

        //生成一个采样  纹理使用的是什么采样器 ，两种 1   普通采样  限定在GPU
//          GL_TEXTURE_2D  GPU内存1     GL_TEXTURE_EXTERNAL_OES 外接设备

        GLES30.glBindTexture(GLES20.GL_TEXTURE_2D, texture)

        GLES30.glUniform1i(vTexture, 0)

        //真正开始渲染
        GLES30.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4)

        return texture
    }

    fun beforeDraw(){

    }
}